package com.jzo2o.orders.manager.strategy.impl;

import cn.hutool.core.bean.BeanUtil;
import com.jzo2o.orders.base.enums.OrderStatusEnum;
import com.jzo2o.orders.base.mapper.OrdersCanceledMapper;
import com.jzo2o.orders.base.model.domain.OrdersCanceled;
import com.jzo2o.orders.base.model.dto.OrderUpdateStatusDTO;
import com.jzo2o.orders.base.service.IOrdersCommonService;
import com.jzo2o.orders.manager.model.dto.OrderCancelDTO;
import com.jzo2o.orders.manager.strategy.OrderCancelStrategy;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

//普通用户取消未支付订单  用户类型:订单类型   UserType:OrderStatusEnum
@Component("0:NO_PAY")
public class CommonSystemNoPayOrderCancelStrategy implements OrderCancelStrategy {

    @Autowired
    private OrdersCanceledMapper ordersCanceledMapper;

    @Autowired
    private IOrdersCommonService ordersCommonService;

    @Override
    public void cancel(OrderCancelDTO orderCancelDTO) {
        //1. 保存取消记录
        OrdersCanceled ordersCanceled = BeanUtil.copyProperties(orderCancelDTO, OrdersCanceled.class);
        ordersCanceled.setCancellerId(orderCancelDTO.getCurrentUserId());//取消人id
        ordersCanceled.setCancelerName(orderCancelDTO.getCurrentUserName());//取消人name
        ordersCanceled.setCancellerType(orderCancelDTO.getCurrentUserType());//取消人类型
        ordersCanceled.setCancelTime(LocalDateTime.now());
        ordersCanceledMapper.insert(ordersCanceled);

        //2. 修改订单状态
        //update orders set orders_status = 600 where id = 订单id and orders_status = 0
        OrderUpdateStatusDTO orderUpdateStatusReqDTO = OrderUpdateStatusDTO.builder()
                .id(orderCancelDTO.getId())//订单id, 更新条件
                .originStatus(OrderStatusEnum.NO_PAY.getStatus())//原态 更新条件
                .targetStatus(OrderStatusEnum.CANCELED.getStatus())//次态 更新字段
                .build();
        ordersCommonService.updateStatus(orderUpdateStatusReqDTO);
    }
}